package com.example.smart_city_0829.activity.lawyer_consultation;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.lawyer_onsultation.LegalExpertiseList;
import com.example.smart_city_0829.utils.Constant;

import java.util.List;

public class LegalExpertiseListActivity extends BaseActivity {

    private GridView gv;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_legal_expertise_list;
    }

    @Override
    protected void initView() {
        gv = (GridView) findViewById(R.id.gv);

    }

    @Override
    protected void initData() {
        final String legalExpertiseListData = getIntent().getStringExtra("LegalExpertiseListData");
        LegalExpertiseList legalExpertiseList = gson.fromJson(legalExpertiseListData,LegalExpertiseList.class);

        if (legalExpertiseList != null){
            gv.setAdapter(new LegalAdapter(legalExpertiseList.getRows(),context));
        }
    }

    @Override
    protected void initEvent() {
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final LegalExpertiseList.RowsBean item = (LegalExpertiseList.RowsBean)parent.getAdapter().getItem(position);
                final Intent intent = new Intent(context, LawyerListActivity.class);
                intent.putExtra("legalExpertiseId",item.getId());
                startActivity(intent);
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "专长选择";
    }

    private class LegalAdapter extends CommonAdapter<LegalExpertiseList.RowsBean, CommonAdapter.CommGridViewHolder> {

        public LegalAdapter(List<LegalExpertiseList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(LegalExpertiseList.RowsBean data, int position, CommGridViewHolder viewHolder, View convertView, Context context) {
            Glide.with(context)
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .into(viewHolder.im);
            viewHolder.serverName.setText(data.getName());
        }

        @Override
        protected CommGridViewHolder getViewHolder() {
            return new CommGridViewHolder();
        }

        @Override
        protected void initView(CommGridViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.serverName = (TextView) convertView.findViewById(R.id.server_name);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.server_list_item;
        }
    }

}